﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Runtime.Remoting.Messaging;

namespace WorkStation.Frameworks.Core
{
    /// <summary>
    /// * 当ApplicationContext用于Web程序中，该HttpModule可以使Context在多线程中共享
    ///   <para>* &lt;httpModules&gt;</para>
    ///   <para>*    &lt;add name="ApplicationContextHttpModule" type="WorkStation.Frameworks.Core.ApplicationContextHttpModule,WorkStation.Frameworks.Core"/&gt;</para>
    ///   <para>* &lt;/httpModules&gt;</para>
    /// </summary>
    public class ApplicationContextHttpModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState);
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }

        void context_PreSendRequestContent(object sender, EventArgs e)
        {
            CallContext.SetData(ApplicationContext.ContextKey, ApplicationContext.Current);
        }

        void context_PostAcquireRequestState(object sender, EventArgs e)
        {
            CallContext.SetData(ApplicationContext.ContextKey, null);
        }

        #endregion
    }
}
